//
//  ActivityInfoHeaderCell.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/5.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "ActivityInfoHeaderCell.h"
#import "SDCycleScrollView.h"
#import "IconView.h"
#import "ActivityInfoModel.h"
#import "SDPhotoBrowser.h"

@interface ActivityInfoHeaderCell()<SDCycleScrollViewDelegate,SDPhotoBrowserDelegate>

// 视图
@property (nonatomic, retain)SDCycleScrollView *bannerView;         // 顶部轮播图
@property (nonatomic, retain)UILabel *titleLabel;                   // 活动标题
@property (nonatomic, retain)IconView *releaseTimeView;             // 活动发布日期
@property (nonatomic, retain)IconView *viewCountView;               // 观看过的次数

@end

@implementation ActivityInfoHeaderCell

#pragma mark - 重写父类方法
-(void)setupView {
    [super setupView];
    // 初始化顶部banner
    self.bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)
                                                         delegate:self
                                                 placeholderImage:[UIImage imageNamed:@"Error"]];
    self.bannerView.infiniteLoop = true;
    self.bannerView.autoScrollTimeInterval = 5.0;
    self.bannerView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    [self.contentView addSubview:self.bannerView];

    // 初始化活动标题
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];

    // 初始化活动发布时间
    self.releaseTimeView = [[IconView alloc] init];
    [self.contentView addSubview:self.releaseTimeView];

    // 初始化已查看的人数
    self.viewCountView = [[IconView alloc] init];
    self.viewCountView.icon = [UIImage imageNamed:@"Eye"];
    [self.contentView addSubview:self.viewCountView];

    // 设置布局
    self.titleLabel.sd_layout.topSpaceToView(self.bannerView,10)
            .leftSpaceToView(self.contentView,5).rightSpaceToView(self.contentView,5).autoHeightRatio(0);

    self.releaseTimeView.sd_layout.topSpaceToView(self.titleLabel,10).leftEqualToView(self.titleLabel).heightIs(14).widthIs(140);

    self.viewCountView.sd_layout.topEqualToView(self.releaseTimeView).leftSpaceToView(self.releaseTimeView,15).heightIs(14).widthIs(60);
}

- (void)setModel:(ActivityInfoModel *)model {
    super.model = model;
    // 设置顶部banner
    self.bannerView.imageURLStringsGroup = model.picture_url;

    // 设置活动标题
    self.titleLabel.text = model.name;

    // 设置活动发布时间
    self.releaseTimeView.text = model.createtime;

    // 设置已查看人数
    self.viewCountView.text = model.looknum;

    [self setupAutoHeightWithBottomView:self.releaseTimeView bottomMargin:5];
}

#pragma mark - SDCycleScrollViewDelegate

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = self.contentView; // 原图的父控件
    browser.imageCount = self.model.picture_url.count; // 图片总数
    browser.currentImageIndex = index;
    browser.delegate = self;
    [browser show];
}

#pragma mark - SDPhotoBrowserDelegate协议
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.picture_url[index]]];
    return imageView.image;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    return [NSURL URLWithString:self.model.picture_url[index]];
}

@end
